﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessObject.DAO
{
    public class BanHocDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<BANLOPHOC> getAllBanHoc()
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                return entities.BANLOPHOCs.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="banhoc"></param>
        public BANLOPHOC insertBanHoc(BANLOPHOC banhoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                entities.BANLOPHOCs.AddObject(banhoc);
                entities.SaveChanges();
                return banhoc;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mon_ban"></param>
        public void insertMonHoc_Ban(MONHOC_BAN mon_ban)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var data = entities.MONHOC_BAN.FirstOrDefault(p => p.BanLopHocId == mon_ban.BanLopHocId && p.MonHocId == mon_ban.MonHocId);
                if (data == null)
                {
                    data = new MONHOC_BAN()
                    {
                        BanLopHocId = mon_ban.BanLopHocId,
                        MonHocId = mon_ban.MonHocId,
                        HeSoDiem = mon_ban.HeSoDiem
                    };
                    entities.MONHOC_BAN.AddObject(data);
                }
                else
                {
                    data.HeSoDiem = mon_ban.HeSoDiem;
                }
                entities.SaveChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="banhoc"></param>
        public void updateBanHoc(BANLOPHOC banhoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var _banhoc = entities.BANLOPHOCs.FirstOrDefault(p => p.BanLopHocId == banhoc.BanLopHocId);
                if (_banhoc == null) return;
                _banhoc.TenBan = banhoc.TenBan;
                entities.SaveChanges();
            }
        }
    }
}
